package com.qk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.dto.ClueQueryDTO;
import com.qk.dto.MarkFalseClueDto;
import com.qk.entity.Business;
import com.qk.entity.Clue;
import com.qk.entity.ClueTrackRecord;
import com.qk.enums.BusinessState;
import com.qk.enums.ClueState;
import com.qk.mapper.BusinessMapper;
import com.qk.mapper.ClueMapper;
import com.qk.mapper.ClueTrackRecordMapper;
import com.qk.service.ClueService;
import com.qk.utils.CurrentUserHoler;
import com.qk.vo.ClueListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * @author 28188
 */
@Service
public class ClueServiceImpl extends ServiceImpl<ClueMapper, Clue> implements ClueService {

    @Autowired
    private ClueTrackRecordMapper clueTrackRecordMapper;
    @Autowired
    private BusinessMapper businessMapper;

    @Override
    public PageResult<ClueListVO> pageClues(ClueQueryDTO clueQueryDTO) {
        Page<ClueListVO> page = this.baseMapper.listClues(new Page<>(clueQueryDTO.getPage(), clueQueryDTO.getPageSize()), clueQueryDTO);
        return new PageResult<>(page.getTotal(), page.getRecords());
    }

    @Override
    public Clue getClueById(Integer id) {
        return this.baseMapper.getClueById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void trackClue(Clue clue) {
        clue.setUserId(CurrentUserHoler.getCurrentUser());
        clue.setStatus(ClueState.FOLLOWING_UP.getCode());
        clue.setUpdateTime(LocalDateTime.now());
        this.updateById(clue);

        ClueTrackRecord clueTrackRecord = new ClueTrackRecord();
        clueTrackRecord.setClueId(clue.getId());
        clueTrackRecord.setUserId(clue.getUserId());
        clueTrackRecord.setSubject(clue.getSubject());
        clueTrackRecord.setLevel(clue.getLevel());
        clueTrackRecord.setRecord(clue.getRecord());
        clueTrackRecord.setNextTime(clue.getNextTime());
        clueTrackRecord.setType(1);
        clueTrackRecord.setCreateTime(LocalDateTime.now());
        clueTrackRecordMapper.insert(clueTrackRecord);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void convertToBusiness(Integer id) {
        Clue clue = this.getById(id);
        clue.setStatus(ClueState.CONVERTED_TO_OPPORTUNITY.getCode());
        clue.setUpdateTime(LocalDateTime.now());
        this.updateById(clue);

        Business business = new Business();
        BeanUtils.copyProperties(clue, business);
        business.setId(null);
        business.setUserId(null);
        business.setNextTime(null);
        business.setStatus(BusinessState.UNASSIGNED.getCode());
        business.setClueId(clue.getId());
        business.setCreateTime(LocalDateTime.now());
        business.setUpdateTime(LocalDateTime.now());

        businessMapper.insert(business);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void falseClueById(Integer id, MarkFalseClueDto markFalseClueDto) {
        Clue clue = new Clue();
        clue.setId(id);
        clue.setStatus(ClueState.FAKE_LEAD.getCode());
        clue.setUpdateTime(LocalDateTime.now());
        this.updateById(clue);

        ClueTrackRecord clueTrackRecord = new ClueTrackRecord();
        clueTrackRecord.setClueId(clue.getId());
        clueTrackRecord.setUserId(CurrentUserHoler.getCurrentUser());
        clueTrackRecord.setCreateTime(LocalDateTime.now());
        clueTrackRecord.setType(0);
        clueTrackRecord.setFalseReason(markFalseClueDto.getReason());
        clueTrackRecord.setRecord(markFalseClueDto.getRemark());
        clueTrackRecordMapper.insert(clueTrackRecord);
    }

    @Override
    public void assignClue(Integer clueId, Integer userId) {
        Clue clue = new Clue();
        clue.setId(clueId);
        clue.setStatus(ClueState.PENDING_FOLLOWUP.getCode());
        clue.setUserId(userId);
        clue.setUpdateTime(LocalDateTime.now());
        this.updateById(clue);
    }

    @Override
    public void addClue(Clue clue) {
        clue.setStatus(ClueState.UNASSIGNED.getCode());
        clue.setCreateTime(LocalDateTime.now());
        clue.setUpdateTime(LocalDateTime.now());
        this.save(clue);
    }

    @Override
    public PageResult<Clue> pagePool(ClueQueryDTO clueQueryDTO) {
        LambdaQueryWrapper<Clue> queryWrapper = new LambdaQueryWrapper<Clue>()
                .eq(clueQueryDTO.getClueId() != null, Clue::getId, clueQueryDTO.getClueId())
                .eq(clueQueryDTO.getPhone() != null && !clueQueryDTO.getPhone().isEmpty(), Clue::getPhone, clueQueryDTO.getPhone())
                .eq(Clue::getStatus, ClueState.FAKE_LEAD.getCode())
                .eq(clueQueryDTO.getChannel() != null, Clue::getChannel, clueQueryDTO.getChannel());

        Page<Clue> page = new Page<>(clueQueryDTO.getPage(), clueQueryDTO.getPageSize());
        Page<Clue> result = this.page(page, queryWrapper);
        return new PageResult<>(result.getTotal(), result.getRecords());
    }

}
